﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_AddFlight : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Calendar1.SelectedDate = DateTime.Now.AddMonths(-11);
            DataView dv = new DataView();
            dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
            DataRowView drv = dv[0];
            //[Total Bussiness Class Seats], [Total First Class Seats], [Total Club Class Seats]

            Label2.Text = drv["Total Bussiness Class Seats"].ToString();
            Label3.Text = drv["Total First Class Seats"].ToString();
            Label4.Text = drv["Total Club Class Seats"].ToString(); 
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (Page.IsValid)
        {
            String AirPlaneName = DropDownList1.SelectedValue.ToString();
            String ScheduleName = DropDownList2.SelectedValue.ToString();
            String isAllowSmoking = cbxSmoking.Checked.ToString();
            DateTime departtime = Calendar1.SelectedDate;

            int index = DropDownList1.SelectedIndex;

            #region get total seat
            DataView dv = new DataView();
            dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
            DataRowView drv = dv[index];
            //[Total Bussiness Class Seats], [Total First Class Seats], [Total Club Class Seats]

            int availBus = Convert.ToInt32(drv["Total Bussiness Class Seats"]);
            int availFirst = Convert.ToInt32(drv["Total First Class Seats"]);
            int availClub = Convert.ToInt32(drv["Total Club Class Seats"]); 
            #endregion

            int index2 = DropDownList2.SelectedIndex;

            DataView dv2 = new DataView();
            dv2 = (DataView)SqlDataSource3.Select(DataSourceSelectArguments.Empty);
            DataRowView drv2 = dv2[index2];

            TimeSpan departhour = TimeSpan.Parse(drv2["DepartTime"].ToString());

            departtime = departtime.Date.Add(departhour);

            AddFlightBAL addFlight = new AddFlightBAL();
            bool result = addFlight.AddFlight(AirPlaneName, ScheduleName, departtime, availBus, availFirst, availClub, txtFlightStatus.Text, cbxSmoking.Checked);
            if (result == true)
            {
                Label1.Text = "Add successfull";
                
                
            }
            else
                Label1.Text = "Erorr";
            
        }
    }

  
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Calendar1.SelectedDate.CompareTo(DateTime.Now) <=0 )
        {
            CustomValidator1.Text = "Please chose depart date that > today";
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = DropDownList1.SelectedIndex;
        DataView dv = new DataView();
        dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
        DataRowView drv = dv[index];
        //[Total Bussiness Class Seats], [Total First Class Seats], [Total Club Class Seats]

        Label2.Text = drv["Total Bussiness Class Seats"].ToString();
        Label3.Text = drv["Total First Class Seats"].ToString();
        Label4.Text = drv["Total Club Class Seats"].ToString();
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        int index = DropDownList2.SelectedIndex;
        DataView dv = new DataView();
        dv = (DataView)SqlDataSource3.Select(DataSourceSelectArguments.Empty);
        DataRowView drv = dv[index];

        string dayofWeek = drv["DayofWeek"].ToString();
        string eDate = e.Day.Date.DayOfWeek.ToString().Remove(3);

        if ((e.Day.Date.Date.CompareTo(DateTime.Now.Date) < 0) || !dayofWeek.Contains(eDate))
        {
            e.Cell.Enabled = false;
            e.Cell.Text = "";
            e.Cell.BackColor = System.Drawing.Color.White;
        }
        
    }
}


